﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebApp.BaseDataSvc;
using YY.Common;

namespace WebApp
{
    public class BasePage : System.Web.UI.Page
    {
        /// <summary>
        /// 数据接口代理
        /// </summary>
        public BaseDataSvc.BaseDataClient BaseDataProxy { get; set; }

        bool _IsPermitFrm = true;
        /// <summary>
        /// 是否授权限制窗体
        /// </summary>
        public bool IsPermitFrm { get { return this._IsPermitFrm; } set { this._IsPermitFrm = value; } }

        /// <summary>
        /// 当前用户模块权限数据
        /// </summary>
        public IEnumerable<TB_ActionPermission> UserPermitDatas = null;

        /// <summary>
        /// 当前窗体所属模块Id,默认null不指定模块Id，使用窗体id作条件绑定权限数据
        /// </summary>
        public int? ModuleId;

        public BasePage()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
            this.BaseDataProxy = new BaseDataSvc.BaseDataClient();
        }


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            //授权操作按钮可用状态控制
            if (!this.IsPermitFrm)//没有受限
                return;

            if (Session["User"] != null)
            {
                TB_User user = Session["User"] as TB_User;
                if (user.role != (int)EnumCommon.SysRoleType.管理员)
                {
                    //检查是否有进入页面的权限
                    var modules = this.BaseDataProxy.GetUserMenus2(user.role, this.GetType().BaseType.Name);
                    if (modules == null | !modules.Any())
                    {
                        Response.Write("没有访问该页面的权限！");
                        Response.End();
                    }
                    //else
                    //this.UserPermitDatas = this.BaseDataProxy.FindActionPermissions(true, user.role, null, null);
                    //initActionCtrls(this, this.UserPermitDatas);
                }
            }
            else
            {
                Response.Write("没能获取登录数据，请先登录系统！");
                Response.End();
            }
        }

        /// <summary>
        /// 检查页面是否对指定操作有权限
        /// </summary>
        /// <param name="op"></param>
        /// <returns></returns>
        public bool IsPermitOP(EnumCommon.PermissionOpType op, string frmId)
        {
            try
            {
                if (!this.IsPermitFrm)//页面没有受限
                    return true;

                if (Session["User"] != null)
                {
                    TB_User user = Session["User"] as TB_User;

                    if (user.role == (int)EnumCommon.SysRoleType.管理员)
                        return true;
                    this.UserPermitDatas = this.BaseDataProxy.FindActionPermissions(true, user.role, null, null);
                    int moduleId = this.BaseDataProxy.FindSysModules(frmId, null).SingleOrDefault().id;
                    if (this.UserPermitDatas != null & this.UserPermitDatas.Any(p => p.opKey.ToLower() == op.ToString().ToLower() & p.moduleId == moduleId))
                        return true;
                }
            }
            catch { }
            return false;
        }

        public override void Dispose()
        {
            base.Dispose();
            if (this.BaseDataProxy != null)
                if (this.BaseDataProxy.State != System.ServiceModel.CommunicationState.Closed)
                    this.BaseDataProxy.Close();
        }
    }
}
